﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using DataAccess;
using Common;
using System.IO;
using System.Data;

namespace BusinessLogic
{
    public class LuuVietHeThong
    {
        public static void LuuViet(string hanhDong, string maNhanVien, string noiDung, string tenDangNhap, string thoiGianThucHien)
        {
            try
            {
                Entities.LuuVietHeThong lv = new Entities.LuuVietHeThong();
                lv.HanhDong = hanhDong;
                lv.MaNhanVien = maNhanVien;
                lv.NoiDung = noiDung;
                lv.TenDangNhap = tenDangNhap;
                lv.ThoiGianThucHien = thoiGianThucHien;
                new LuuVietHeThong().LuuViet(lv);
            }
            catch
            {
                return;
            }
        }
        public void LuuViet(Entities.LuuVietHeThong lvht)
        {
            Stream stream = null;
            try
            {
                System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                DataTable dtUsers = null;
                if (File.Exists(Environment.CurrentDirectory + @"\LogFile"))
                {
                    stream = File.OpenRead("LogFile");
                    if (stream.Length != 0)
                    {
                        dtUsers = (DataTable)formatter.Deserialize(stream);
                    }
                    if (dtUsers == null)
                    {
                        dtUsers = new DataTable();
                        //column 1
                        DataColumn column = new DataColumn();
                        column.DataType = typeof(String);
                        column.ColumnName = Common.Constants.LuuVietHeThong.MaNhanVien;
                        dtUsers.Columns.Add(column);
                        //column 2
                        DataColumn column2 = new DataColumn();
                        column2.DataType = typeof(String);
                        column2.ColumnName = Common.Constants.LuuVietHeThong.TenDangNhap;
                        dtUsers.Columns.Add(column2);
                        //column 3
                        DataColumn column3 = new DataColumn();
                        column3.DataType = typeof(String);
                        column3.ColumnName = Common.Constants.LuuVietHeThong.HanhDong;
                        dtUsers.Columns.Add(column3);
                        //column 4
                        DataColumn column4 = new DataColumn();
                        column4.DataType = typeof(DateTime);
                        column4.ColumnName = Common.Constants.LuuVietHeThong.ThoiGianThucHien;
                        dtUsers.Columns.Add(column4);
                        //column 5
                        DataColumn column5 = new DataColumn();
                        column5.DataType = typeof(String);
                        column5.ColumnName = Common.Constants.LuuVietHeThong.NoiDung;
                        dtUsers.Columns.Add(column5);

                        //Add Row
                        DataRow row = dtUsers.NewRow();
                        row[Common.Constants.LuuVietHeThong.MaNhanVien] = lvht.MaNhanVien;
                        row[Common.Constants.LuuVietHeThong.TenDangNhap] = lvht.TenDangNhap;
                        row[Common.Constants.LuuVietHeThong.HanhDong] = lvht.HanhDong;
                        row[Common.Constants.LuuVietHeThong.ThoiGianThucHien] = DateTime.Now.ToShortDateString();
                        row[Common.Constants.LuuVietHeThong.NoiDung] = lvht.NoiDung;
                        dtUsers.Rows.Add(row);
                    }
                    else
                    {
                        DataRow row = dtUsers.NewRow();
                        row[Common.Constants.LuuVietHeThong.MaNhanVien] = lvht.MaNhanVien;
                        row[Common.Constants.LuuVietHeThong.TenDangNhap] = lvht.TenDangNhap;
                        row[Common.Constants.LuuVietHeThong.HanhDong] = lvht.HanhDong;
                        row[Common.Constants.LuuVietHeThong.ThoiGianThucHien] = DateTime.Now;
                        row[Common.Constants.LuuVietHeThong.NoiDung] = lvht.NoiDung;
                        dtUsers.Rows.Add(row);
                    }
                    stream.Close();
                }
                else
                {
                    dtUsers = new DataTable();
                    //column 1
                    DataColumn column = new DataColumn();
                    column.DataType = typeof(String);
                    column.ColumnName = Common.Constants.LuuVietHeThong.MaNhanVien;
                    dtUsers.Columns.Add(column);
                    //column 2
                    DataColumn column2 = new DataColumn();
                    column2.DataType = typeof(String);
                    column2.ColumnName = Common.Constants.LuuVietHeThong.TenDangNhap;
                    dtUsers.Columns.Add(column2);
                    //column 3
                    DataColumn column3 = new DataColumn();
                    column3.DataType = typeof(String);
                    column3.ColumnName = Common.Constants.LuuVietHeThong.HanhDong;
                    dtUsers.Columns.Add(column3);
                    //column 4
                    DataColumn column4 = new DataColumn();
                    column4.DataType = typeof(DateTime);
                    column4.ColumnName = Common.Constants.LuuVietHeThong.ThoiGianThucHien;
                    dtUsers.Columns.Add(column4);
                    //column 5
                    DataColumn column5 = new DataColumn();
                    column5.DataType = typeof(String);
                    column5.ColumnName = Common.Constants.LuuVietHeThong.NoiDung;
                    dtUsers.Columns.Add(column5);

                    //Add Row
                    DataRow row = dtUsers.NewRow();
                    row[Common.Constants.LuuVietHeThong.MaNhanVien] = lvht.MaNhanVien;
                    row[Common.Constants.LuuVietHeThong.TenDangNhap] = lvht.TenDangNhap;
                    row[Common.Constants.LuuVietHeThong.HanhDong] = lvht.HanhDong;
                    row[Common.Constants.LuuVietHeThong.ThoiGianThucHien] = DateTime.Now;
                    row[Common.Constants.LuuVietHeThong.NoiDung] = lvht.NoiDung;
                    dtUsers.Rows.Add(row);
                }
                stream = File.Open("LogFile", FileMode.Create, FileAccess.Write);
                formatter.Serialize(stream, dtUsers);
                dtUsers.Clear();
                dtUsers.Dispose();

            }
            catch
            {

            }
            if (stream != null)
                stream.Close();
        }

        public Entities.LuuVietHeThong[] SelectLuuViet()
        {
            Stream stream = null;
            Entities.LuuVietHeThong[] lvhtarr = null;
            try
            {
                if (File.Exists(Environment.CurrentDirectory + @"\LogFile"))
                {
                    stream = File.Open("LogFile", FileMode.Open, FileAccess.Read);
                    System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    DataTable dtUsers = null;
                    if (stream.Length != 0)
                    {
                        dtUsers = (DataTable)formatter.Deserialize(stream);
                        List<Entities.LuuVietHeThong> arr = new List<Entities.LuuVietHeThong>();
                        foreach (DataRow item in dtUsers.Rows)
                        {
                            DateTime dt = Convert.ToDateTime(item[3]);

                                Entities.LuuVietHeThong lvht = new Entities.LuuVietHeThong(item[0].ToString(), item[1].ToString(), item[2].ToString(), dt.ToString("dd/MM/yyyy hh:mm tt"), item[4].ToString());
                                arr.Add(lvht);
                        }
                        if (arr.Count > 0)
                        {
                            lvhtarr = new Entities.LuuVietHeThong[arr.Count];
                            int y = 0;
                            for (int i = arr.Count-1; i >=0 ; i--)
                            {
                                lvhtarr[y++] = arr[i];
                            }
                        }
                        else
                            lvhtarr = new Entities.LuuVietHeThong[0];
                    }
                    else
                        lvhtarr = new Entities.LuuVietHeThong[0];
                }
                else
                    lvhtarr = new Entities.LuuVietHeThong[0];
            }
            catch
            {
                lvhtarr = new Entities.LuuVietHeThong[0];
            }
            if (stream != null)
                stream.Close();
            return lvhtarr;
        }

        public void Xoa()
        {
            lock (this)
            {
                Stream stream = null;
                try
                {
                    if (File.Exists(Environment.CurrentDirectory + @"\LogFile"))
                    {
                        stream = new FileStream("LogFile", FileMode.Create, FileAccess.ReadWrite);
                    }

                }
                catch
                {

                }
                if (stream != null)
                    stream.Close();
            }
        }
    }
}
